﻿Imports FinanceiroBLL
Imports VendasBLL
Imports FinanceiroDAL.ModuloConexao
Public Class ContasReceberUC
    Dim objBLL As ContasReceberBLL
    Dim sta As Integer = 0
    Dim x As String
#Region "TSBLocalizar"
    Private Sub TSBLocalizar_Click(sender As System.Object, e As System.EventArgs)
        If (PNFiltros.Visible = True) Then
            PNFiltros.Visible = False
        Else
            PNFiltros.Visible = True
            TSBLocalizar.Checked = True
        End If
    End Sub
#End Region
#Region "TSBVoltar"
    Private Sub TSBVoltar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        statusfinanceiro = "N"
        Dispose()
        objBLL = Nothing
    End Sub
#End Region
#Region "KeyPress"
    Private Sub CBConsultas_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles CBFiltros.KeyPress
        e.Handled = True
    End Sub
#End Region
#Region "Cor de fundo do datagridview"
    Private Sub DGVDados_RowPrePaint(sender As System.Object, e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DGVDados.RowPrePaint
        '   Me.DGVDados.CurrentRow.Selected = False 'tira a seleção da primeira linha da dgvdados
        Try
            If DGVDados.Rows.Count > 0 Then
                If (DGVDados.Rows(e.RowIndex).Cells("sta_descricao").Value = "Aberto") And (DGVDados.Rows(e.RowIndex).Cells("icr_dtvencimento").Value = Date.Now.ToShortDateString) Then
                    DGVDados.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Yellow 'hoje
                End If
                If (DGVDados.Rows(e.RowIndex).Cells("sta_descricao").Value = "Aberto") And (DGVDados.Rows(e.RowIndex).Cells("icr_dtvencimento").Value > Date.Now.ToShortDateString) Then
                    DGVDados.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.White 'não vencidas (futuro)
                End If
                If (DGVDados.Rows(e.RowIndex).Cells("sta_descricao").Value = "Aberto") And (DGVDados.Rows(e.RowIndex).Cells("icr_dtvencimento").Value < Date.Now.ToShortDateString) Then
                    DGVDados.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Salmon 'vencidos
                End If
                If (DGVDados.Rows(e.RowIndex).Cells("sta_descricao").Value = "Fechado") And (DGVDados.Rows(e.RowIndex).Cells("sta_codigo").Value = 6) Then
                    DGVDados.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.PaleGreen 'pagos
                End If
            End If
        Catch ex As Exception
        End Try
    End Sub
#End Region
#Region "CBFiltros"
    Private Sub CBFiltros_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBFiltros.SelectedIndexChanged
        If CBFiltros.SelectedIndex = 3 Then
            CBStatus.Visible = True
            TXTLocalizar.Visible = False
            CBStatus.SelectedIndex = 0
            CBStatus_SelectedIndexChanged(sender, e)
        Else
            CBStatus.Visible = False
            TXTLocalizar.Visible = True
            TXTLocalizar.Focus()
            CarregaGrid()
        End If
        If CBFiltros.SelectedIndex = 1 Then
            TXTLocalizar.Visible = False
            DTPInicial.Visible = True
            DTPFinal.Visible = True
            LBInformacao.Visible = True
            BBuscar.Visible = True
        Else
            BBuscar.Visible = False
            DTPInicial.Visible = False
            DTPFinal.Visible = False
            LBInformacao.Visible = False
        End If
        TXTLocalizar.Text = ""
    End Sub
#End Region
#Region "Load"
    Private Sub ContasReceberUC_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Try
            CBFiltros.SelectedIndex = 2
            CarregaGrid()
            DGVDados.ReadOnly = True
        Catch ex As Exception
        End Try
    End Sub
#End Region
#Region "CarregaGRid"
    Sub CarregaGrid()
        objBLL = New ContasReceberBLL
        DGVDados.DataSource = objBLL.localizar("", 7)
        DGVDados.Refresh()

    End Sub
#End Region
#Region "Localiza textChanged"
    Private Sub TXTLocalizar_TextChanged(sender As System.Object, e As System.EventArgs) Handles TXTLocalizar.TextChanged
        objBLL = New ContasReceberBLL
        Try
            Select Case CBFiltros.Text
                Case "Código Venda"
                    DGVDados.DataSource = objBLL.localizar(TXTLocalizar.Text, CBFiltros.SelectedIndex)
                Case "Data Vencimento"
                    BBuscar_Click(Nothing, Nothing)
                Case "Cliente"
                    DGVDados.DataSource = objBLL.localizar(TXTLocalizar.Text, CBFiltros.SelectedIndex)
                Case "Status"
                    DGVDados.DataSource = objBLL.localizar(TXTLocalizar.Text, CBFiltros.SelectedIndex)
            End Select
            If TXTLocalizar.Text = "" Then
                DGVDados.DataSource = objBLL.localizar("", 8)
            End If
            DGVDados.Refresh()
        Catch ex As Exception
            MsgBox("Erro no filtro", ex.Message)
        End Try
    End Sub
#End Region
#Region "Localiza DAtas"
    Private Sub BBuscar_Click(sender As System.Object, e As System.EventArgs) Handles BBuscar.Click
        Try
            objBLL = New ContasReceberBLL
            DGVDados.DataSource = objBLL.localizadata(DTPInicial.Text, DTPFinal.Text)
            DGVDados.Refresh()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub
#End Region
#Region "CBStatus"
    Private Sub CBStatus_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBStatus.SelectedIndexChanged
        objBLL = New ContasReceberBLL
        DGVDados.DataSource = objBLL.localizar(CBStatus.Text, CBFiltros.SelectedIndex)
        DGVDados.Refresh()
    End Sub
#End Region
#Region "TSBLocalizar"
    Private Sub TSBLocalizar_Click_1(sender As System.Object, e As System.EventArgs) Handles TSBLocalizar.Click
        If PNFiltros.Visible = True Then
            PNFiltros.Visible = False
            TXTLocalizar.Text = ""
            TXTLocalizar.Visible = True
            TSBLocalizar.BackColor = Color.Transparent
        Else
            PNFiltros.Visible = True
            TXTLocalizar.Text = ""
            TSBLocalizar.BackColor = Color.Gainsboro
        End If
        TXTLocalizar.Select()
    End Sub
#End Region
#Region "Keypress"
    Private Sub TXTLocalizar_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTLocalizar.KeyPress
        If CBFiltros.SelectedIndex = 1 Then
            If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
                e.Handled = True
            End If
        End If
    End Sub
#End Region
#Region "TSBNovo"
    Private Sub TSBNovo_Click(sender As System.Object, e As System.EventArgs) Handles TSBNovo.Click

        Dim frm As New Contas_ReceberFRM
        frm.Op = "N" 'novo
        TSBNovo.BackColor = Color.Gainsboro
        frm.ShowDialog()
        CarregaGrid()
        TSBNovo.BackColor = Color.Transparent
        frm.Dispose()
        frm = Nothing
    End Sub
#End Region
#Region "Visualizar"
    Private Sub TSBVisualizar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVisualizar.Click
        Dim indice As Integer = -1
        Dim frm As New VisualizaFRM
        Try
            For Each linha As DataGridViewRow In DGVDados.SelectedRows
                If (Not linha.Cells("sta_codigo") Is Nothing) Then
                    indice = linha.Index
                End If
            Next
            If (indice <> -1) Then
                Dim Informacao As String
                If DGVDados.Rows.Count > 0 Then
                    Informacao = DGVDados.CurrentRow.Cells("ctr_codigo").Value.ToString()
                    frm.cod = Informacao
                    frm.ShowDialog()
                    frm.Dispose()
                    frm = Nothing
                End If
            Else
                MessageBox.Show("Sem registro para seleção", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch
        End Try
    End Sub
#End Region
#Region "Receber"
    Private Sub TSBReceber_Click(sender As System.Object, e As System.EventArgs) Handles TSBReceber.Click
        Dim indice As Integer = -1
        Try
            For Each linha As DataGridViewRow In DGVDados.SelectedRows
                If (Not linha.Cells("ctr_codigo") Is Nothing) Then
                    indice = linha.Index
                    Dim ind2 As Integer = 1
                    If (Not linha.Cells("icr_codigo") Is Nothing) Then
                        ind2 = linha.Index
                    End If
                End If
            Next
            If (indice <> -1) Then
                sta = Val(DGVDados.Rows(indice).Cells("sta_codigo").Value)
                If sta <> 6 Then
                    If DGVDados.Rows.Count > 0 Then
                        Dim frm As New RecebimentosFRM
                        frm.Codigo = Val(DGVDados.Rows(indice).Cells("ctr_codigo").Value)
                        frm.Codigoit = Val(DGVDados.Rows(indice).Cells("icr_codigo").Value)
                        frm.dtemissao = DGVDados.Rows(indice).Cells("ctr_dtemissao").Value
                        frm.Op = "R" 'recebimento
                        TSBReceber.BackColor = Color.Gainsboro
                        frm.ShowDialog()
                        frm.Dispose()
                        frm = Nothing
                        CarregaGrid()

                    Else
                        MsgBox("Nenhuma linha encontrada!")
                    End If
                Else
                    MsgBox("Parcela já recebida!", MsgBoxStyle.Exclamation)

                End If
            Else
                MessageBox.Show("Não existe registro para seleção!", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Exception
            '   MessageBox.Show(ex.Message)
        Finally
            TSBReceber.BackColor = Color.Transparent
        End Try
    End Sub
#End Region
#Region "Tools Strip"
    Private Sub ToolStripButton1_Click(sender As System.Object, e As System.EventArgs)
        Dim fr As New RecParcelamentosFRM
        fr.ShowDialog()
    End Sub
#End Region
#Region "Editar"
    Private Sub TSBEditar_Click(sender As System.Object, e As System.EventArgs) Handles TSBEditar.Click
        Try
            Dim objitBLL As New itensCtasReceberBLL
            For Each linha As DataGridViewRow In DGVDados.SelectedRows
                If (Not linha.Cells("ctr_codigo") Is Nothing) Then
                    objitBLL = New itensCtasReceberBLL(Val(DGVDados.Rows(linha.Index).Cells("icr_codigo").Value.ToString()), 1)
                End If
            Next
            Dim cad As New RecParcelamentosFRM
            If (objitBLL.icrcodigo > 0 And objitBLL.stacodigo = 4) Then
                cad.LCodigo.Text = objitBLL.ctrcodigo
                cad.Op = "E" 'edita uc
                cad.codctr = objitBLL.ctrcodigo
                cad.LCodigo.Visible = False
                TSBEditar.BackColor = Color.Gainsboro
                cad.ShowDialog()
                CarregaGrid()
                cad.Dispose()
                cad = Nothing
            Else
                MessageBox.Show("Lançamento finalizado. Não pode ser alterado.", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
                x = "I"
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Erro:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Finally
            TSBEditar.BackColor = Color.Transparent
        End Try
    End Sub
#End Region
#Region "Double click"
    Private Sub DGVDados_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVDados.CellDoubleClick
        TSBEditar_Click(Nothing, Nothing)
    End Sub
#End Region
#Region "Detalhes"
    Public Sub gerarelatrio(titulo As String, formula As String)
        Try
            Dim frm As New RelatoriosfinFRM
            Dim rpt As New ContasReceberDetalhadoREL

            Try
                rpt.SetDatabaseLogon(usuario, senha)
                rpt.SetParameterValue(0, titulo)
                rpt.RecordSelectionFormula = formula
                frm.CrystalReportViewer2.ShowGroupTreeButton = False
                frm.CrystalReportViewer2.ReportSource = rpt
                frm.ShowDialog()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
            frm.Dispose()
            rpt = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Finally

        End Try
    End Sub
    Private Sub TSBDetalhes_Click_1(sender As System.Object, e As System.EventArgs) Handles TSBDetalhes.Click
        Dim objcli As New ClientesBLL
        Dim nome As String
        Dim objitBLL As New itensCtasReceberBLL
        For Each linha As DataGridViewRow In DGVDados.SelectedRows
            If (Not linha.Cells("icr_codigo") Is Nothing) Then
                objitBLL = New itensCtasReceberBLL(Val(DGVDados.Rows(linha.Index).Cells("icr_codigo").Value.ToString()), 0)
            End If
        Next
        If (Not IsNothing(objitBLL)) Then
            objBLL = New ContasReceberBLL(objitBLL.ctrcodigo, 6)
            objcli = New ClientesBLL(objBLL.clicodigo)
            nome = objcli.Nome
            If (objitBLL.icrcodigo > 0) Then gerarelatrio("Contas à Receber de " & nome, "{vw_FinContasReceber.icr_codigo}=" & objitBLL.icrcodigo)
        End If
    End Sub
#End Region
End Class
